//
//  DecoratingVC.m
//  ErWa
//
//  Created by 赵颜彬 on 16/3/9.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DecoratingVC.h"
#import "DecoratingCell.h"
#import "DecoratingModel.h"
#import <UIImageView+WebCache.h>
#import "AFNetworking.h"
#import "DecoratingDetailPageVC.h"
#import "NewNestHeader.h"
@interface DecoratingVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger number;/**< 用来保存加载时动态值 */
@end

@implementation DecoratingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // navigation
    self.navigationItem.title = @"正在装修";
    
     [self dataHandler];
    
    [self createSubviews];
    
   
}


/// 数据处理
- (void)dataHandler
{
    //collectionview数据
    NSString *str = @"http://v5.owner.mjbang.cn/api/live-shows/new-list?page=1&app_version=2.1.3&user_token=af9d7efbcf93fba063f4f45fbf442c73-111249-App%5CModels%5CMember&phone_type=iOS&limit=10&app_channel=appstore&imei=0ABFB620-74E5-4ADF-AA87-C92326CFA2CD&mark=1&app_type=Owner";
    [AFNetworking GETWithURL:str completion:^(id result) {
        NSMutableArray *arr = result[@"data"];
        self.dataArr = [DecoratingModel modelHanderWithArray:arr];
        [_collectionview reloadData];
        NSLog(@"%@",self.dataArr);
    }];
}
/// 视图创建
- (void)createSubviews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30) / 2, self.view.frame.size.width / 3);
    layout.sectionInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44) collectionViewLayout:layout];
    _collectionview.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _collectionview.backgroundColor = [UIColor whiteColor];
    _collectionview.delegate = self;
    _collectionview.dataSource = self;
    // 判断当前是什么模式
    BOOL model = [AccountManager sharedInstance].isNight;
    if (model == YES) {
        self.collectionview.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        self.view.backgroundColor = [UIColor changeColorWithString:@"#a1a3a6"];
        
    } else {
        self.collectionview.backgroundColor = [UIColor clearColor];
        self.view.backgroundColor = [UIColor clearColor];
        
    }
    
    [_collectionview registerClass:[DecoratingCell class] forCellWithReuseIdentifier:@"cell"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_collectionview];
    
}

/// 设置cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

/// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DecoratingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (_dataArr.count != 0) {
        DecoratingModel *m = _dataArr[indexPath.row];
        cell.titleLabel.text = m.title;
        NSString *str = [NSString stringWithFormat:@"%@㎡",m.acreage_property];
        cell.spaceLabel.text = str;
        cell.contentLabel.text = m.content;
        [cell.backImg sd_setImageWithURL:[NSURL URLWithString:m.imgStr] placeholderImage:[UIImage imageNamed:@"heng"]];
        return cell;
    }else{
        return cell;
    }
}


// 点击进入详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DecoratingDetailPageVC *detailVC = [[DecoratingDetailPageVC alloc] init];
    DecoratingModel *m = _dataArr[indexPath.item];
    detailVC.detailId = m.detailId;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
